package hikari.gui.panels.particle;

import com.gooddies.events.ValueChangedEvent;
import com.gooddies.swing.RowLayout;
import com.gooddies.texteditors.DefaultFloatTextField;
import hikari.gui.guicomponents.hLabel;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import org.swig.jirr.dimension2df;

/**
 * @author sad
 */
public class Dimension2dEditor {
    private DefaultFloatTextField x;
    private DefaultFloatTextField y;
    private ActionListener valueChanged;
    private boolean disableInput = false;

    public Dimension2dEditor(JPanel panel) {
        int width = 35;
        panel.add(new hLabel("X:"));
        x = new DefaultFloatTextField();
        x.setPreferredSize(new Dimension(width, 20));
        panel.add(x);
        x.setValueChangedEventNumber(getAction());
        panel.add(new hLabel("Y:"));
        y = new DefaultFloatTextField();
        y.setPreferredSize(new Dimension(width, 20));
        panel.add(y, RowLayout.NewRow);
        y.setValueChangedEventNumber(getAction());
    }

    public void setData(dimension2df data) {
        disableInput = true;
        x.setValue(data.getWidth());
        y.setValue(data.getHeight());
        disableInput = false;
    }

    public dimension2df getData() {
        return new dimension2df((float) x.getValue(), (float) y.getValue());
    }

    public void setValueChangedEvent(ActionListener valueChanged) {
        this.valueChanged = valueChanged;
    }

    private ValueChangedEvent<Float> getAction() {
        return new ValueChangedEvent<Float>() {
            @Override
            public void valueChanged(Float value) {
                if (valueChanged != null && disableInput == false) {
                    valueChanged.actionPerformed(null);
                }
            }
        };
    }

}
